< previous page page_212 next page >

Page 212
Functions to Test Data Types
Sometimes you may need to check the type of data a user has typed into a text box. For example, suppose youre expecting the user to type in a date or some numeric data. Well, a text box control accepts all characterstheres no automatic way to mask the data so that only a certain type of data comes in. Therefore, you may need to check the data to make sure the user input the correct type. You can do this with a couple of functions presented in this section as shown in Figure 10.4.
8160-0212a.gif
Figure 10.4
Test data type example.
Examples Filename: TestDataType.frm
IsNumeric( )
The IsNumeric() function will return True if the value passed to it can be converted to a number. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdIsNumeric_Click()
   If IsNumeric(txtNumeric.Text) Then
      MsgBox "Value is Numeric"
   End If
End Sub
IsDate( )
IsDate() returns True if the value passed to it is a valid date. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdIsDate_Click()
   If IsDate(txtDate.Text) Then
      MsgBox "Value is a Date"
   End If
End Sub
9f31265bb40b592b1929e18a47ab28e5.gif
CAUTION
When
IsDate() is combined with CVDate(), Visual Basic can consider a string such as 334466 as a valid date. Make sure you always check variant and strings with IsDate() prior to converting them.

 
< previous page page_212 next page >

If you like this book, buy it!